package com.farm.wcp.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.farm.wcp.client.domain.ApiResult;
import com.farm.wcp.client.utils.HttpUtils;

/**
 * 数据查询相关业务
 * 
 * @author macpl
 *
 */
public class WcpDataClient {
	private Map<String, String> urls;
	private String secret;
	private String operatorLoginname;
	private String operatorPassword;
	private String baseUrl;

	public WcpDataClient(String baseUrl, String secret, String operatorLoginname, String operatorPassword) {
		super();
		this.secret = secret;
		this.baseUrl = baseUrl;
		this.operatorLoginname = operatorLoginname;
		this.operatorPassword = operatorPassword;
	}

	/**
	 * 填充密钥和接口用户信息
	 * 
	 * @param para
	 */
	private void initBaseParas(Map<String, String> para) {
		para.put("operatorLoginname", operatorLoginname);
		para.put("operatorPassword", operatorPassword);
		para.put("secret", secret);
	}

	private Map<String, String> getApiUrls() {
		urls = new HashMap<>();
		urls.put("getKnows", baseUrl + "/get/know.do");
		urls.put("getFqas", baseUrl + "/get/fqa.do");
		return urls;
	}

	/**
	 * 知识查询
	 * 
	 * @param categoryid
	 * @param domtype
	 * @param sortfield
	 * @param sorttype
	 * @param pagesize
	 * @return
	 */
	public ApiResult getKnows(String categoryid, String domtype, String sortfield, String sorttype,
			Integer pagesize) {
		Map<String, String> para = new HashMap<String, String>();
		initBaseParas(para);
		para.put("categoryid", categoryid);
		para.put("domtype", domtype);
		para.put("sortfield", sorttype);
		para.put("sorttype", sorttype);
		if (pagesize != null) {
			para.put("pagesize", pagesize.toString());
		}
		JSONObject json = HttpUtils.httpPost(getApiUrls().get("getKnows"), para);
		WcpClient.validateBackJson(getApiUrls().get("getKnows"), json);
		return new ApiResult(json);
	}
	
	
	/**
	 * 问答查询
	 * 
	 * @param categoryid
	 * @param domtype
	 * @param sortfield
	 * @param sorttype
	 * @param pagesize
	 * @return
	 */
	public ApiResult getFqas(String categoryid, String domtype, String sortfield, String sorttype,
			Integer pagesize) {
		Map<String, String> para = new HashMap<String, String>();
		initBaseParas(para);
		para.put("categoryid", categoryid);
		para.put("domtype", domtype);
		para.put("sortfield", sorttype);
		para.put("sorttype", sorttype);
		if (pagesize != null) {
			para.put("pagesize", pagesize.toString());
		}
		JSONObject json = HttpUtils.httpPost(getApiUrls().get("getFqas"), para);
		WcpClient.validateBackJson(getApiUrls().get("getFqas"), json);
		return new ApiResult(json);
	}
}
